<template>
  <el-dialog title="添加商品" :close-on-click-modal="false" :visible.sync="showAddIndexImgtDialog">
    <el-form ref="addIndexImgFrom" :model="addIndexImgFrom" label-width="120px">
        <el-form-item label="轮播图类型" prop="indexType">
            <el-radio-group v-model="addIndexImgFrom.indexType">
                <el-radio v-model="radio" label="1">分类</el-radio>
                <el-radio label="2">商品</el-radio>
            </el-radio-group>
            <!-- <el-radio v-model="radio" label="1">分类</el-radio>
            <el-radio v-model="radio" label="2">商品</el-radio> -->
        </el-form-item>
        <el-form-item label="名称" prop="categoryId">
            <!-- props可以设置数据的lable和value为自定义属性 -->
           <el-cascader
            style="width: 100%;"
            :props="{value: 'id', label: 'name'}"
            v-model="addIndexImgFrom.categoryId"
            :options="categories"></el-cascader>
        </el-form-item>

        <el-form-item label="商品图片" prop="img">
        <el-upload
            action="api.addImg"
            list-type="picture-card"
            :http-request="upload"
            :limit="limit"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>          
        </el-form-item>
    
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="showAddIndexImgtDialog = false">取 消</el-button>
        <el-button type="primary" :loading="sureBtnLoading" @click="sureSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import http from '@/utils/request'
export default {
    props: {
      categories:{
          type:Array,
      },
      limit: {
          type: Number,
          default: 1
			}
    },           
    data() {
        return {
            radio: '1',
            dialogImageUrl: '',
            fileList: [],
            imageUrl: '',
            api:{addImg:'http://localhost:8082/eduoss/fileOss1'},

            dialogVisible:false,
            sureBtnLoading: false,
            showAddIndexImgtDialog: false,
            addIndexImgFrom: {
                indexType: '',
                categoryId: ''
            },
        }
    },
    methods:{
      upload(fileObj) {
        this.fileList.push(fileObj)
          console.log("==2");
          let fileList =[]
          fileList.push(fileObj)
          let formdata = new FormData()
          fileList.forEach(el =>{
              console.log(el);
              if(el){
                    formdata.append('file',el.file)
              }
            })
            console.log(formdata);
          http.post('/index-img/backend/fileOss',
            formdata, {
                headers: {
                'Content-Type': 'multipart/form-data'
                }
            }).then(res => {
              let imgurl = res.data.data
              window.console.log("------------------------------------------------");
              window.console.log(imgurl);
                    this.fileList.push({url:res.data.data[0]})
                })
      },
        sureSubmit(){
                this.$refs.addIndexImgFrom.validate(valid => {
               if(valid) {
                //    window.console.log(valid);
                  //  this.sureBtnLoading = true
                  this.sureBtnLoading = true 
                  let formdata = {}
                   formdata.categoryId = this.addIndexImgFrom.categoryId[this.addIndexImgFrom.categoryId.length - 1]
                   formdata.indexType = this.addIndexImgFrom.indexType
                  
                  // console.log(this.fileList[this.fileList.length-1].url);

                   formdata.url=this.fileList[this.fileList.length-1].url


                   http.post('/index-img/add',formdata 
                   ).then(() => {
                        this.sureBtnLoading = false
                        this.showAddIndexImgtDialog = false
                        this.$message({
                          message: '数据添加成功',
                            type: 'success'
                        });
                        this.$refs.addIndexImgFrom.resetFields()
                        this.$emit('refresh')
                   })
               }
           })
        },
        handleRemove(file, fileList) {
        this.fileList = fileList
      }
    }
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>